package br.com.admi.core.persistence.dao.hibernate;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Y2XG
 * Date: 19/03/13
 * Time: 16:03
 * To change this template use File | Settings | File Templates.
 */
public class AbstractHibernateDAOImpl<T> {

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    private Class<T> type;

    /**
     * Salvar um objeto no banco de dados
     */
    public void save(T entity) {

        // Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        // Save
        session.save(entity);
    }

    /**
     * Retrieves all persons
     *
     * @return a list of persons
     */
    public List<T> getAll() {

        // Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(type);
        return criteria.list();
    }

}
